package httpapi

import (
	"strconv"

	"away.wei/minidevpos/global"
	"away.wei/minidevpos/model"
	"github.com/gin-gonic/gin"
)

type ProjectTaskApi struct{}

// @Tags Task
// @Summary 添加任务
// @Security ApiKeyAuth
// @Param project_id query int true "编号"
// @Router /project/task/add [get]
func (s *ProjectTaskApi) Add(c *gin.Context) {
	projectId, _ := strconv.Atoi(c.Query("project_id"))
	if _, err := projectTaskRep.Create(projectId); err != nil {
		FailWithMessage(err.Error(), c)
	} else {
		OkWithMessage("执行成功", c)
	}
}

// @Tags Task
// @Summary 取消任务
// @Security ApiKeyAuth
// @Param project_id query int true "编号"
// @Router /project/task/cancel [get]
func (s *ProjectTaskApi) Cancel(c *gin.Context) {
	projectId, _ := strconv.Atoi(c.Query("project_id"))
	global.DevOpsEventAPP.CancelTask(projectId)
	OkWithMessage("成功", c)
}

// @Tags Task
// @Summary 分页查询
// @Security ApiKeyAuth
// @Param data query model.ProjectTaskSearch true " "
// @Router /project/task/getpage [get]
func (s *ProjectTaskApi) GetPageByProjectId(c *gin.Context) {
	var dto model.ProjectTaskSearch
	_ = c.ShouldBindQuery(&dto)
	list, total, err := projectTaskRep.GetPage(dto)

	if err != nil {
		FailWithMessage("获取失败", c)
	} else {
		OkWithDetailed(PageResult{
			List:     list,
			Total:    total,
			Page:     dto.Page,
			PageSize: dto.PageSize,
		}, "获取成功", c)
	}
}

// @Tags Task
// @Summary 删除任务
// @Security ApiKeyAuth
// @Param ids body []int true "编号"
// @Router /project/task [delete]
func (s *ProjectTaskApi) Delete(c *gin.Context) {
	var ids []int
	c.ShouldBindJSON(&ids)
	if err := projectTaskRep.Delete(ids); err != nil {
		FailWithMessage(err.Error(), c)
	} else {
		OkWithMessage("删除成功", c)
	}
}
